import { Dialog } from "vant";
import yzService from "../../components/ui_components/yz_service.vue";
import yzGoodsposter from "components/ui_components/yz_goodsPoster";
import { scrollMixin } from "../../utils/mixin";
export default {
  mixins: [scrollMixin],
  components: { yzService, yzGoodsposter },
  data() {
    return {
      show: false,
      list: [],
      order_status_name:"",
      order_status:null,

      touchable: true,
      // 客服弹窗
      showKefu: false,
      cservice: "",
      service_QRcode: "",
      service_mobile: "",

      info: {
        has_one_group: {
          has_one_goods: {
            thumb: "",
            market_price: ""
          }
        },
        has_one_level: { group_price: "" }
      },
      order_pay_id: "",

      goods_list: [],
      isLoadMore: true,
      page: 1,
      total_page: 0,
      order_id:"",

      showShare: false,
      options: [
        { name: '邀请好友', icon: 'link' },
        { name: '微信', icon: 'wechat' },
        { name: '分享海报', icon: 'poster' }
      ],

      //海报显隐
      posterShow: false,
      posterImg: null,
      isFrontCanvas: false, //是否前端控制生成海报，false为后端生成
    }
  },
  activated() {
    try {
      let meta_referrer = document.querySelector('#meta_referrer');
      meta_referrer.setAttribute('content', 'no-referrer');
    } catch (error) {
      console.log(error);
    }

    this.isLoadMore = true;
    this.order_pay_id = this.$route.query.pay;
    this.getData();
  },
  methods: {
    getMoreData(){
      this.isLoadMore = false;
      if(this.page>=this.total_page){
        return;
      }
      let page = this.page+1;
      this.getGoods(page);
    },
    getGoods(page) {
      $http.get("payment.success-page.goods-page", { order_id: this.order_id, page }).then(res => {
        this.isLoadMore = true;
        if (res.result !== 1) {
          return this.$toast(res.msg);
        }
        this.page = res.data.current_page;
        this.total_page = res.data.last_page;
        if(page==1){
          this.goods_list = res.data.data;
        }else{
          this.goods_list = this.goods_list.concat(res.data.data);
        }
      })
    },
    handle_mousewheel() {
      this.touchable = false;
    },
    show_kefu(data) {
      let json = {};
      if (data.page_type !== 'hotel' && data.page_type !== 'live-install') {
        json.plugin = "store";
        json.store_id = data.store.id;
      }
      $http.get("member.service.api", json).then(res => {
        if (res.result !== 1) {
          return this.$toast(res.msg);
        }
        this.cservice = res.data.cservice;
        this.service_QRcode = res.data.service_QRcode;
        this.service_mobile = res.data.service_mobile;
        this.showKefu = true;
      })
    },
    group_open(data) {
      $http.get('plugin.fight-groups.frontend.controllers.team.index', { team_id: data.team_id }, "").then(res => {
        if (res.result !== 1) {
          return this.$toast(res.msg);
        }
        let json = {
          id: res.data.group_id
        }
        if (data.store_id) {
          json.store_id = data.store_id;
        }
        this.$router.push(this.fun.getUrl("GroupGoods", json));

      })

    },
    formatTime(time){
      let _time = time.replace(/-/g,"/");
      return _time;
    },
    //分享
    shareWeixin(data) {
      let _u = `group_detail/${data.team_id}`;
      if (data.store_id) {
        _u += `/${data.store_id}`;
      }
      let url = window.location.href.replace("orderPaySuccess", _u);

      if (this.fun.getTyep() == "5") {
        this.$copyText(`${data.has_one_group.desc_content}${url}`).then((e)=> {
          this.$toast("复制成功");
        }, (e) => {
          this.$toast("复制失败");
        });
        return;
      }

      this.fun.wxShare(
        "",
        {},
        {
          link: url,
          title: data.has_one_group.desc_title,
          imgUrl: data.has_one_group.goods_thumb,
          description: data.has_one_group.desc_content
        }
      );
      this.$dialog.alert({ message: "请点击右上角微信分享" });
    },
    showShareSheet(data){
      this.sheetExt = data;
      this.showShare = true;

    },
    onSelectSheet(option){
      let _u = `group_detail/${this.sheetExt.team_id}`;
      if (this.sheetExt.store_id) {
        _u += `/${this.sheetExt.store_id}`;
      }
      let url = window.location.href.replace("orderPaySuccess", _u);

      if(option.icon == 'link') {
        this.$copyText(`${this.sheetExt.has_one_group.desc_content}${url}`).then((e)=> {
          this.$toast("复制成功");
        }, (e) => {
          this.$toast("复制失败");
        });
      }else if(option.icon == 'wechat'){
        this.fun.wxShare(
          "",
          {},
          {
            link: url,
            title: this.sheetExt.has_one_group.desc_title,
            imgUrl: this.sheetExt.has_one_group.goods_thumb,
            description: this.sheetExt.has_one_group.desc_content
          }
        );
        this.$dialog.alert({ message: "请点击右上角微信分享" });
      } else if(option.icon == 'poster'){
        this.postShow();
      }
    },
    async postShow() {
      let { result, msg, data } = await $http.get('plugin.fight-groups.frontend.controllers.team.get-poster',{id: this.sheetExt.team_id},".");
      if (result == 0) return this.$toast(msg);
      this.posterImg = this.isFrontCanvas ? data : `${data.image_url}?${Date.now()}`;
      this.posterShow = true;
    },
    getData() {
      $http.get("payment.success-page.index", { order_pay_id: this.order_pay_id }, '').then(res => {
        if (res.result !== 1) {
          return this.$toast(res.msg);
        }
        this.order_id = res.data[0].public_param.order_id;
        this.list = res.data;
        let json = {
          '-1': this.$i18n.t('base.closed'),
          '0': this.$i18n.t('base.unpaid'),
          "1": this.$i18n.t('base.paid'),
          "2": this.$i18n.t('base.shipped'),
          "3": this.$i18n.t('base.transactionCompleted')
        }
        this.fun.setWXTitle(json[res.data[0].public_param.order_status]);
        this.order_status_name = json[res.data[0].public_param.order_status];
        this.order_status =  res.data[0].public_param.order_status;
        if(this.order_id&&res.data[0].goods&&res.data[0].goods.length!==0){
          this.page = res.data[0].goods.current_page;
          this.total_page = res.data[0].goods.last_page;
        }
      })
    },
    banner_link(url) {
      if (url) {
        window.location.href = url;
      }
    },
    tips_handle() {
      Dialog.alert({
        message: '订单支付后赠送需要等订单支付后才发放，订单完成后赠送的需要等订单确认收货后才发放，每月1号赠送需要等每月1号才发放，奖励数量请以最终实际发放数量为准',
      }).then(() => {
        // on close
      });
    },
    sotre_handle(data) {
      if (data.store.specify_show == 0) {
        this.$router.push(this.fun.getUrl("HomeSeller", { store_id: data.store.id }))
      } else {
        this.$router.push(this.fun.getUrl("o2oStore_v2", { store_id: data.store.id }))
      }
    },
    format_lat_lng(data) {
      if (data.page_type == 'store-cashier' && data.is_qr_code == 2) {
        return {
          latitude: data.store.latitude*1,
          longitude: data.store.longitude*1,
          storeName: data.store.full_address,
        }
      } else {
        return {
          latitude: data.deliver.lat*1,
          longitude: data.deliver.lng*1,
          storeName: data.deliver.full_address,

        }
      }
    },
    navigation_1({ latitude, longitude, storeName }) {
      let json = {
      lng :longitude*1,
      lat :latitude*1
    }
      this.fun.goToWXAdress(json, storeName);
    },
    navigation(...params) {
      let [latitude, longitude, storeName] = [...params];
      let json = {
        lng :longitude*1,
        lat :latitude*1
      }
      this.fun.goToWXAdress(json, storeName);
    },
    change_order(index) {
      this.order_id = this.list[index].order_info.order_id;
      let height = Math.ceil(document.querySelectorAll(".section")[index].offsetHeight);
      this.order_height = height + 'px';
      this.page = 1;
      this.total_page=this.list[index].goods.last_page;;
      // this.getGoods(1);
    },
    goto(name, data) {
      if (name == 'course') {//课程
        this.$router.push(
          this.fun.getUrl("CourseDetail", { id: data.goods_id })
        );
      } else if (name == 'hotel') { //酒店
        this.$router.push(this.fun.getUrl("HotelHome", { id: data.id, fromHome: 1 }));
      } else if (name == 'goods') {
        this.$router.push(this.fun.getUrl("goods", { id: data.id }));
      } else if (name == 'couponShare') {
        this.$router.push(this.fun.getUrl(name, { order_id: data.order_id }));
      } else {
        if (name == 'coupon_v2_detail') {
          this.$router.push(this.fun.getUrl(name, { id: data.id }))
        } else {
          this.$router.push(this.fun.getUrl(name))
        }
      }
    }
    }
}